package Z07JDBC.DB01HelloJDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author 李某
 * * @date 2021/10/20
 * *物有本末，事有终始。知其先后者，则近道矣！
 * *荷花开满池塘的前一天还是半塘荷花半塘云。
 */

// 数据导入成功

public class DB01QuDongImpoet {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("驱动初始化成功");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

// 连接数据库

class TestJDBC{
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8",
                    "root", "root");
            System.out.println("初始化成功！" + "连接对象" + c);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}


class TestJDBC1{
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8",
                    "root", "root");
            Statement s = c.createStatement();
            System.out.println("获取 Statement对象：" + s);
            System.out.println("初始化成功！" + "连接对象" + c);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}


class TestJDBC2{
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8",
                    "root", "root");
            Statement s = c.createStatement();
            System.out.println("获取 Statement对象：" + s);
            System.out.println("初始化成功！" + "连接对象" + c);

            String sql = "insert into hero values(null,"+"'提莫'"+","+323.0f +"," + 50 +")";

            s.execute(sql);
            System.out.println("执行插入语句成功！");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}


class TestJDBC3{
    public static void main(String[] args) {
        Connection c = null;
        Statement s = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8",
                    "root", "root");
            s = c.createStatement();
            System.out.println("获取 Statement对象：" + s);
            System.out.println("初始化成功！" + "连接对象" + c);

            String sql = "insert into hero values(null,"+"'提莫'"+","+323.0f +"," + 50 +")";

            s.execute(sql);
            System.out.println("执行插入语句成功！");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            if (s != null)
                try {
                    s.close();
        } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
        }
}
}


class TestJDBC4{
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try (
            Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8",
                    "root", "root");
            Statement s = c.createStatement();
        ){
            String sql = "insert into hero values(null," + "'提莫'" + "," + 313.0f + "," + 50 + ")";
            s.execute(sql);
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}

